package com.LC.Controller.BKManagement;

import com.LC.Constant.MessageConstant;
import com.LC.Entity.OrderSetting;
import com.LC.Entity.Result;
import com.LC.Service.OrderSettingService;
import com.LC.Utils.POIUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 预约设置
 */
@RestController
@RequestMapping("/OrderSetting")
public class OrderSettingController {

    @Reference
    OrderSettingService orderSettingService;

    //文件上传，实现预约设置数据批量导入
    @PreAuthorize("hasAuthority('ORDERSETTING')")
    @RequestMapping("/Up")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile){
        try {
            //使用POI解析表格数据
            List<String[]> readExcel = POIUtils.readExcel(excelFile);
            List<OrderSetting> data = new ArrayList<>();
            for (String[] strings : readExcel) {
                String orderDate = strings[0];
                String number = strings[1];
                OrderSetting orderSetting = new OrderSetting(new Date(orderDate),Integer.valueOf(number));
                data.add(orderSetting);
            }
            //通过dubbo远程调用服务实现数据批量导入到数据库
                orderSettingService.add(data);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }

    /**
     * 由月份查询对应的预约设置数据
     * @return
     */
    @PreAuthorize("hasAuthority('ORDERSETTING')")
    @RequestMapping("/OrderSettingByMonth")
    public Result getOrderSettingByMonth(String date){  //date格式为 yyyy-MM
        List<Map> mapListByMouth;
        try {
            mapListByMouth = orderSettingService.getOrderSettingByMonth(date);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }
        return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,mapListByMouth);
    }

    @PreAuthorize("hasAuthority('ORDERSETTING')")
    @RequestMapping("/OrderSettingByButton")
    public Result OrderSettingByButton(@RequestBody OrderSetting orderSetting){
        try {
            orderSettingService.OrderSettingByButton(orderSetting);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
        return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
    }

}
